<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.part_name" placeholder="配件名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getParts">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="parts" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="part_name" label="配件名称"  sortable>
			</el-table-column>
			<el-table-column prop="price" label="配件价格"  sortable>
			</el-table-column>
			<el-table-column prop="num" label="配件数量"  sortable>
			</el-table-column>
			<el-table-column prop="warn_num" label="配件预警"  sortable>
			</el-table-column>
			<el-table-column prop="context" label="配件描述"  sortable>
			</el-table-column>
			<el-table-column prop="create_time" label="配件入库时间"  sortable>
			</el-table-column>


			<el-table-column label="操作" width="200" >
				<template slot-scope="scope">
					<el-button type="success" size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--分页工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10" :total="total" style="float:right;">
			</el-pagination>
		</el-col>





		<!--修改的时候 弹出 编辑界面-->
		<el-dialog title="编辑"  v-model="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
				<el-form-item label="配件名称" prop="part_name">
					<el-input v-model="editForm.part_name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="配件价格" prop="price">
					<el-input v-model="editForm.price" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="配件数量" prop="num">
					<el-input v-model="editForm.num" auto-complete="off"></el-input>
				</el-form-item>
				<!--<el-form-item label="配件预警" prop="warn_num">
					<el-input v-model="editForm.warn_num" auto-complete="off"></el-input>
				</el-form-item>-->
				<el-form-item label="配件描述" prop="context">
					<el-input type="textarea"  v-model="editForm.context" auto-complete="off"></el-input>
				</el-form-item>

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

		<!--新增界面-->
		<el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="配件名称" prop="part_name">
					<el-input v-model="addForm.part_name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="配件价格" prop="price">
					<el-input v-model="addForm.price" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="配件数量" prop="num">
					<el-input v-model="addForm.num" auto-complete="off"></el-input>
				</el-form-item>
				<!--<el-form-item label="配件预警" prop="warn_num">
					<el-input v-model="addForm.warn_num" auto-complete="off"></el-input>
				</el-form-item>-->
				<el-form-item label="配件描述" prop="context">
					<el-input type="textarea"  v-model="addForm.context" auto-complete="off"></el-input>
				</el-form-item>


			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>

	export default {
		data() {
			return {
                filters:{
                    id:'',
                    part_name:'',
                    price:'',
                    num:'',
                    warn_num:'',
                    context:'',
                    create_time:''
				},

                dialogVisible: false,

				//分页绑定值
				currentPage:1,
				total:0,
                sels: [],//列表选中列
                listLoading:false,//如果等于false 现在加载中效果
                editLoading:false,//修改加载中
				addLoading:false,//新增加载
                parts:[],
                editFormVisible:false,//默认不显示对话框
                addFormVisible:false,//新增对话框 是否显示 false 不显示新增对话框
                editForm:{
                    part_name:'',
                    price:'',
                    num:'',
                    warn_num:'',
                    context:''
				},//修改form对象
                editFormRules: {
                    part_name: [
                        { required: true, message: '请输入配件名称', trigger: 'blur' }
                    ],
                    price: [
                        { required: true, message: '请输入配件价格', trigger: 'blur' }
                    ],
                    num: [
                        { required: true, message: '请输入配件数量', trigger: 'blur' }
                    ],
                    warn_num: [
                        { required: true, message: '请输入配件预警数量', trigger: 'blur' }
                    ],
                    context: [
                        { required: true, message: '请输入配件描述', trigger: 'blur' }
                    ]
                },

                addForm:{

                    part_name:'',
                    price:'',
                    num:'',
                    warn_num:'',
                    context:''

				},//新增form对象
                addFormRules: {
                    part_name: [
                        { required: true, message: '请输入配件名称', trigger: 'blur' }
                    ],
                    price: [
                        { required: true, message: '请输入配件价格', trigger: 'blur' }
                    ],
                    num: [
                        { required: true, message: '请输入配件数量', trigger: 'blur' }
                    ],
                    warn_num: [
                        { required: true, message: '请输入配件预警数量', trigger: 'blur' }
                    ],
                    context: [
                        { required: true, message: '请输入配件描述', trigger: 'blur' }
                    ]
                },
			}
		},
		methods: {
		    //分页方法
            handleCurrentChange(val){
                //当前页
				this.currentPage = val;
				this.getParts();
			},
		    //查询所有的部门
            getParts(){
				//发送axios请求到后端 查询部门
				// http://localhost:80/department/list
				let param = {
                    part_name:this.filters.part_name,
				    currentPage:this.currentPage
				}
				this.$http.patch('/part/queryPage',param).then(res =>{
                    this.total = res.data.total;
					this.parts = res.data.rows;
				})
			},
			//新增弹出对话框
            handleAdd(){
                //弹出表单
				this.addFormVisible =true;
			},
			//编辑弹出对话框
            handleEdit(index,row){
                //打开修改的对话框
				this.editFormVisible = true;
				//回显
                this.editForm = Object.assign({}, row);

			},
			//删除方法
            handleDel(index,row){
                let id = row.id
				this.$http.delete('/part/'+id).then(res =>{

					console.log(res.data);
					 let {success,message} = res.data;
					 if (success){
                         this.$message({
                             message: res.data.massage,
                             type: 'success'
                         });
                         this.getParts();
					 }else{
                         this.$message({
                             message: res.data.massage,
                             type: 'error'
                         });
					 }
				})
			},
			//新增保存操作
            //新增
            addSubmit: function () {
                //提交之前的验证工作--elementui验证
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            //出现加载中效果
                            this.addLoading = true;

                            let para = Object.assign({}, this.addForm);
                            console.debug(para);
                            this.$http.post('/part/add',para).then((res) => {
                                //禁止转圈圈
                                this.addLoading = false;
                                let {success,message}=res.data;
                                if (success) {
                                    this.$message({
                                        message: res.data.massage,
                                        type: 'success'
                                    });
                                    //表单清空
                                    this.$refs['addForm'].resetFields();
                                    //关闭对话框
                                    this.addFormVisible = false;
                                    this.getParts();
                                }else{
                                    this.$message({
                                        message: res.data.massage,
                                        type: 'error'
                                    });
								}
                            });
                        });
                    }
                });
            },
			//新增保存
			editSubmit(){

                //和新增类似
                //提交之前的验证工作--elementui验证
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            //出现加载中效果
                            this.editLoading = true;
                            //{name:"xxx"}把新增表单里面form 拷贝到 {}
                            let para = Object.assign({}, this.editForm);
                            this.$http.post('/part/update',para).then((res) => {
                                //去掉转圈圈
                                console.debug(para.part_name);
                                console.debug(para.price);
                                console.debug(para.num);
                                console.debug(para.context);

                                this.editLoading = false;
                                let {success,message}=res.data;
                                if (success) {
                                    this.$message({
                                        message: res.data.massage,
                                        type: 'success'
                                    });
                                    //表单清空
                                    this.$refs['editForm'].resetFields();
                                    //关闭对话框
                                    this.editFormVisible = false;
                                    this.getParts();
                                }else{
                                    this.$message({
                                        message: res.data.massage,
                                        type: 'error'
                                    });
                                }
                            });
                        });
                    }
                });

			},
			//多选方法
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;

                    let para = { ids: ids };
                    this.$http.patch('/part/batchDelete?ids='+ids).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: res.data.massage,
                            type: 'success'
                        });
                        this.getParts();
                    });
                }).catch(() => {

                });
            }

		},
		mounted() {
			//挂载结束 el:#myDiv 所有vue内容 都渲染到DOM--虚拟DOM
			//发送请求
			this.getParts()
		}
	}

</script>

<style scoped>

</style>